<!-- create-menu -->
<template>
  <div class="create-or-update">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ isEdit ? '编辑菜单' : '新增菜单' }}</span>
      </div>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径">
          <el-input v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="form.parentId" placeholder="请选择活动区域">
            <!-- 无一级菜单 -->
            <el-option
              label="无一级菜单"
              :value="-1"
            ></el-option>
            <!-- 一级菜单 -->
            <el-option
              v-for="item in parentMenuList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="前端图标" prop="icon">
          <el-input v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-radio-group v-model="form.shown">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.orderNum"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="cancel()" v-if="isEdit">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getEditMenuInfo, addOrUpdateMenu } from '@/services/menu'
export default {
  name: 'createOrUpdate',
  props: {
    isEdit: {
      type: Boolean,
      // 默认添加功能
      default: false
    }
  },
  created () {
    // 加载上级菜单
    this.loadMenuInfo()
  },
  data () {
    return {
      parentMenuList: [],
      form: {
        parentId: -1,
        name: '',
        href: '',
        icon: '',
        orderNum: 0,
        description: '',
        shown: true
      },
      rules: {
        name: { required: true, message: '请输入菜单的名称', trigger: 'blur' },
        description: { required: true, message: '请输入描述内容', trigger: 'blur' },
        icon: { required: true, message: '请输入前端图标', trigger: 'blur' }
      }
    }
  },
  methods: {
    async onSubmit () {
      // 1.表单验证
      try {
        await this.$refs.form.validate()
        // 2.新增菜单
        const { data } = await addOrUpdateMenu(this.form)
        if (data.code === '000000') {
          this.$message.success('新增菜单成功')
          this.$router.push({
            name: 'menu'
          })
        }
      } catch {
        console.log('校验不通过')
      }
    },
    // 取消
    cancel () {
      this.$router.push({
        name: 'menu'
      })
    },
    async loadMenuInfo () {
      // 通过路由传值，判断是新增还是编辑菜单
      const id = this.$route.params.id || -1
      // 请求成功，获取一级菜单
      const { data } = await getEditMenuInfo(id)
      if (data.code === '000000') {
        // 将上级菜单数据保存，进行数据绑定
        this.parentMenuList = data.data.parentMenuList
        // 检查是否存在菜单数据menuInfo
        if (data.data.menuInfo) {
          this.form = data.data.menuInfo
        }
      }
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
